package xiangya.bysj.util;

import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {
	private final static String REGEX = "^[a-z0-9\\_]+$";
	private final static String RESOURCE = "xiangya-config.properties";
	private final static String FILE_TYPE = "fileType";
	
	public final static String RES_SUCCESS = "success";
	public final static String RES_FAIL = "fail";
	public final static String RES_CKVERCODE_FAIL = "ck_vercode_fial";
	
	public final static String DATE_FORMAT = "yyyy-MM-dd";
	public final static String HOUR_FOMAT = "HH:mm";
	
	public static boolean judgeUsername(String username){
		Pattern pattern = Pattern.compile(REGEX);
		Matcher matcher = pattern.matcher(username);
		return matcher.find();
	}
	
	/**
	 * 生成唯一的文件名
	 * @param doc 后缀名
	 * @return 文件名
	 */
	public static String getFileName(String doc){
		UUID uuid = UUID.randomUUID();
		return uuid.toString().replace("-", "")+"."+doc;
	}
	
	/**
	 * 获取字段的set方法
	 * @param fieldName 字段名
	 * @return 返回set方法名
	 */
	public static String getMethodName(String fieldName){
		byte[] items = fieldName.getBytes();
		items[0] = (byte)((char)items[0]-'a'+'A');;
		return "set" + new String(items);
	}
	
	/**
	 * 过滤文件
	 * @param doc 文件后缀名
	 * @return 返回是否过滤
	 */
	public static boolean filterFileType(String doc){
		return PropertyUtil.getValue(RESOURCE, FILE_TYPE).contains(doc.toLowerCase());
	}
}
